package wq.gdky005.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * SQLite 的工具类
 * @author wangqing
 * 2013-5-27
 */
public abstract class DBHelper extends SQLiteOpenHelper{

	/** 数据库的版本号  */
	public static int VERSION = 1;
	/** 数据库的名字  */
	public String dbname = null;

	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		dbname = name;
	}

	public DBHelper(Context context, String name, CursorFactory factory) {
		this(context, name, factory, VERSION);
		dbname = name;

	}

	public DBHelper(Context context, String name, int version) {
		this(context, name, null, version);
		dbname = name;
	}

	public DBHelper(Context context, String name) {

		this(context, name, null);
		dbname = name;
	}

	@Override
	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
		initDB(paramSQLiteDatabase);
	}
	

	/**
	 * 初始化数据库
	 * wangqing
	 * 2013-5-27
	 * void
	 * @param paramSQLiteDatabase
	 */
	public abstract void initDB(SQLiteDatabase paramSQLiteDatabase);

	/**
	 * 通用查询
	 * 
	 * wangqing
	 * 2013-5-27
	 * Cursor
	 * @param tablename
	 *            表名
	 * @param cols
	 *            列名
	 * @param sqlwhere
	 *            条件
	 * @param sqlwherevalues
	 *            条件值
	 * 
	 */
	public Cursor query(String tablename, String[] cols, String sqlwhere,
			String[] sqlwherevalues, String orderBy) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(tablename, cols, sqlwhere, sqlwherevalues,
				null, null, orderBy);

		return cursor;

	}

	/**
	 * 执行SQL语句
	 * wangqing
	 * 2013-5-27
	 * Cursor
	 * @param sql
	 * @return
	 */
	public Cursor query(String sql) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 通用插入
	 * 
	 * @param tablename
	 * @param contentValues
	 */
	public long insert(String tablename, ContentValues contentValues) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.insert(tablename, null, contentValues);

	}

	/**
	 * 通用更新
	 * 
	 * @param tablename
	 * @param contentValues
	 * @param sqlwhere
	 * @param sqlwherevalues
	 */
	public long update(String tablename, ContentValues contentValues,
			String sqlwhere, String[] sqlwherevalues) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.update(tablename, contentValues, sqlwhere, sqlwherevalues);
	}

	/**
	 * 通用删除
	 * 
	 * @param sql
	 */
	public long delete(String table, String whereClause, String[] whereArgs) {
		SQLiteDatabase db = this.getWritableDatabase();

		return db.delete(table, whereClause, whereArgs);
	}
	
	/**
	 * 更新数据库
	 * wangqing
	 * 2013-5-27
	 * void
	 */
	public void upgradeDatabase() {
		SQLiteDatabase db = this.getWritableDatabase();

		onUpgrade(db, VERSION - 1, VERSION);
	}

	/** 数据库更新  */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

}
